<?php
import( 'de.ceus-media.alg.ColorConverter' );
import( 'de.ceus-media.ui.Template' );
import( 'de.ceus-media.ui.html.PageFrame' );
import( 'de.ceus-media.net.http.request.Receiver' );
final class ColorPicker
{
	protected $fileColorTable	= "colors.png";

	public function __construct( $timeLimit = 30 )
	{
		set_time_limit( $timeLimit );

		$request	= new Net_HTTP_Request_Receiver;
		$posX		= 0;
		$posY		= 0;
		$html		= $request->has( 'html' ) ? $request->get( 'html' ) : NULL; 
		$rgb		= $request->has( 'rgb' ) ? $request->get( 'rgb' ) : NULL; 
		$hsv		= $request->has( 'hsv' ) ? $request->get( 'hsv' ) : NULL; 

		if( !file_exists( $this->fileColorTable ) )
		{
			$this->generateColorTable();
		}

		if( $html )
		{
			$rgb	= Alg_ColorConverter::html2rgb( $html );
			$hsv	= Alg_ColorConverter::rgb2hsv( $rgb );
		}
		else if( $rgb )
		{
			$rgb	= explode( ",", $rgb );
			$method	= create_function( '$a', 'return trim( $a );' );					//  define Callback to trim spaces
			$rgb	= array_map( $method, $rgb );										//  trim spaces
			$html	= Alg_ColorConverter::rgb2html( $rgb );
			$hsv	= Alg_ColorConverter::html2hsv( $html );
		}
		else if( $hsv )
		{
			$hsv	= explode( ",", $hsv );
			$method	= create_function( '$a', 'return trim( $a );' );					//  define Callback to trim spaces
			$hsv	= array_map( $method, $hsv );										//  trim spaces
		}
		if( $hsv )
		{
			$posX	= $hsv[0];
			$posY	= $hsv[1];
			if( $hsv[2] < 100 )
				$posY	+= 100 - $hsv[2];
		}
		else
		{
			$posX	= $request->has( 'x' ) ? $request->get( 'x' ) : 0;
			$posY	= $request->has( 'y' ) ? $request->get( 'y' ) : 0;
		}
		$data	= $this->buildTemplateData( $posX, $posY, $html, $rgb, $hsv );
		$html	= $this->buildViewHtml( $data );
		echo $html;
	}

	protected function buildTemplateData( $posX, $posY, $html, $rgb, $hsv )
	{
		if( !$hsv )
		{
			$s		= $posY < 100 ? $posY : 100;
			$v		= $posY < 100 ? 100 : 100 - ( $posY - 100 );
			$hsv	= array( $posX, $s, $v );
		}
		if( !$rgb )
		{
			$rgb	= Alg_ColorConverter::hsv2rgb( $hsv );
		}
		if( !$html )
		{
			$html	= Alg_ColorConverter::rgb2html( $rgb );
		}

		$data	= array(
			'posX'		=> $posX - 2,
			'posY'		=> $posY - 8,
			'file'		=> $this->fileColorTable,
			'html'		=> $html,
			'rgb'		=> $rgb,
			'hsv'		=> $hsv,
		);
		return $data;
	}
	
	protected function buildViewHtml( $data )
	{
		$body	= UI_Template::render( 'template.html', $data );
		$page	= new UI_HTML_PageFrame();
		$page->setTitle( 'ColorPicker < cmTools' ); 
		$page->addStylesheet( "//css.ceusmedia.com/blueprint/reset.css" );
		$page->addStylesheet( "//css.ceusmedia.com/blueprint/typography.css" );
		$page->addStylesheet( "./style.css" );
		$page->addBody( $body );
		return $page->build();
	}

	protected function generateColorTable()
	{
		$image	= imageCreateTrueColor( 360, 200 );
		for( $i=0; $i<360; $i++ )
		{
			for( $j=1; $j<=100; $j++ )
			{
				$rgb	= Alg_ColorConverter::hsv2rgb( array( $i, $j, 100 ) );
				$col	= imageColorAllocate( $image, $rgb[0], $rgb[1], $rgb[2] );
				imageSetPixel( $image, $i, $j-1, $col );
			}
			for( $k=1; $k<=100; $k++ )
			{
				$rgb	= Alg_ColorConverter::hsv2rgb( array( $i, 100, 100-$k ) );
				$col	= imageColorAllocate( $image, $rgb[0], $rgb[1], $rgb[2] );
				imageSetPixel( $image, $i, $k-1+100, $col );
			}
		}
		imagePNG( $image, $this->fileColorTable );
		imageDestroy( $image );
	}
}
?>